unit DBService;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes, TCPService,
  ContextFactory, MemBuffer, DBClientContext, TCPEngine, TCPListener, DBConnect;

type
  TDBService = class(TTCPService)
  private
    FDBConnect: TDBConnection;
    procedure SetDBConnect(ADBConnect: TDBConnection);
  public
    function OnCreateClient: TIClientContext; override;
    procedure AppendDisplayMsg(pFormat: PChar; const Args: array of const);
    procedure AppendLogMessage(pFormat: PChar; const Args: array of const);
    procedure AppendErrorLogMessage(pFormat: PChar; const Args: array of const);
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  published
    property DBConnect: TDBConnection read FDBConnect write SetDBConnect;
  end;

  procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('TIOCP', [TDBService]);
end;

{ TFTPService }

procedure TDBService.Notification(AComponent: TComponent;
  Operation: TOperation);
begin
  inherited;
  if Operation = opRemove then
  begin
    if (AComponent = FDBConnect) then FDBConnect:= nil;
  end;
end;

procedure TDBService.AppendDisplayMsg(pFormat: PChar;
  const Args: array of const);
begin

end;

procedure TDBService.AppendErrorLogMessage(pFormat: PChar;
  const Args: array of const);
begin

end;

procedure TDBService.AppendLogMessage(pFormat: PChar;
  const Args: array of const);
begin

end;

function TDBService.OnCreateClient: TIClientContext;
begin
  Result := TIDBClientContext.Create(_TCPEngine);
  if Assigned(FDBConnect) then
  begin
    (Result as TIDBClientContext).DBConnect := Self.FDBConnect;
  end;
end;

procedure TDBService.SetDBConnect(ADBConnect: TDBConnection);
begin
  if Assigned(FDBConnect) then
    DBConnect.RemoveFreeNotification(Self);
  FDBConnect := ADBConnect;
  if Assigned(FDBConnect) then
    FDBConnect.FreeNotification(Self);
end;

end.
